package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.MusicListM;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

public class HealthMessListActivity extends BaseActivity {

    @BindView(R.id.rlist_health)
    RecyclerView rlistHealth;
    @BindView(R.id.refresh_health)
    SwipeRefreshLayout mRefresh;
    ArrayList<MusicListM.RowsBean> Temp_list = new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;
    private HealthListAdapter mAdapter;
    int page = 1;
    private LinearLayout li_health_null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_mess_list);
        ButterKnife.bind(this);
        ChangLayTitle("运动健康知识");
        LayBack();
        init();
        getData();
    }

    public void init() {
        //空
        li_health_null = (LinearLayout) findViewById(R.id.li_health_null);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        linearLayoutManager = new LinearLayoutManager(this);
        rlistHealth.setLayoutManager(linearLayoutManager);
        mAdapter = new HealthListAdapter(HealthMessListActivity.this, rlistHealth, Temp_list, R.layout.item_text);
        rlistHealth.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent in = new Intent(HealthMessListActivity.this, HealthMessXQActivity.class);
                in.putExtra("id", Temp_list.get(position + 1).getCourseId());
                startActivity(in);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }

        });

    }


    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.VideoList, Const.POST);
        mRequest.add("page", page);
        mRequest.add("fileType", "3");
        // 添加到请求队列
        CallServer.getRequestInstance().add(HealthMessListActivity.this, 0, mRequest,
                new CustomHttpListener(HealthMessListActivity.this, true, MusicListM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        MusicListM model = ((MusicListM) data);
                        if (page == 1) {
                            Temp_list.clear();
                        }
                        Temp_list.addAll(model.getRows());
                        if (Temp_list.size() == 0) {
                            li_health_null.setVisibility(View.VISIBLE);
                            rlistHealth.setVisibility(View.GONE);
                        } else {
                            li_health_null.setVisibility(View.GONE);
                            rlistHealth.setVisibility(View.VISIBLE);
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }

    public class HealthListAdapter extends BaseLoadMoreHeaderAdapter<MusicListM.RowsBean> {


        public HealthListAdapter(Context mContext, RecyclerView recyclerView, List<MusicListM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, MusicListM.RowsBean s) {
            View view_ban = (View) holder.itemView.findViewById(R.id.view_text_ban);
            View view_man = (View) holder.itemView.findViewById(R.id.view_text_man);
            TextView tv_text_tilte = (TextView) holder.itemView.findViewById(R.id.tv_text_tilte);
            tv_text_tilte.setText(s.getTitle());
            if (holder.getLayoutPosition() == Temp_list.size() - 1) {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.VISIBLE);
            } else {
                view_ban.setVisibility(View.VISIBLE);
                view_man.setVisibility(View.GONE);
            }
        }
    }
}
